<template>
	<view class="content" v-if="show==1">
		<view class="main">
			<radio-group @change="moren2">
				<label class="xuanze" v-for="(p,index) in price">
					<radio :value="index" color="#05c160" style="transform:scale(0.7)" />{{p.title}}
				</label>
			</radio-group>
			<!-- #ifdef APP-PLUS -->
			<view class="info" v-if='zdtype==0'>
				<view class="info2"><text>*</text>支付方式</view>
				<view class="info3">
					<radio-group @change="moren" style="text-align: right;line-height: 45px;">
						<label style="margin-right: 30px;">
							<radio value="2" color="#05c160" style="transform:scale(0.7)" />支付宝支付
						</label>
						<label>
							<radio value="1" color="#05c160" style="transform:scale(0.7)" />微信支付
						</label>
					</radio-group>
				</view>
			</view>
			<!--  #endif -->
			<view class="zhifu"></view>
			<!-- #ifdef MP-WEIXIN -->
			<view class="info4" @click="shouquan">
				立即续费<text v-if="money>0">（{{zdtype==1?'㉿':'￥'}}{{money}}）</text>
			</view>
			<!-- #endif -->
			<!-- #ifndef MP-WEIXIN -->
			<view class="info4" @click="baocun" v-if="zdtype==0">
				立即续费<text v-if="money>0">（{{zdtype==1?'㉿':'￥'}}{{money}}）</text>
			</view>
			<!-- #endif -->
			<view class="info4" @click="chong" v-if="zdtype==1 && user.money<money">
				余额不足(㉿{{user.money}})，去充值
			</view>
			<view class="info4" @click="baocun" v-if="zdtype==1 && user.money>=money">
				立即续费<text v-if="money>0">（{{zdtype==1?'㉿':'￥'}}{{money}}）</text>
			</view>
		</view>
	</view>
</template>

<script>
	var app=getApp();
	export default {
		data() {
			return {
				show:0,
				uid:0,
				code:'',
				zdtype:0,
				paytype:0,
				money:0,
				price:[],
				user:[],
				id:0
			}
		},
		onLoad() {
			const uid = uni.getStorageSync('uid');
			if(app.isBlank(uid)==true){
				app.error_msg("请先登录");
			}
			var that=this;
			uni.getSystemInfo({
			    success: function (res) {
					// #ifdef APP-PLUS
					if(res.platform=='ios'){
						//that.zdtype=1;
					}
					// #endif
			    }
			});
			this.uid=uid;
			this.getdata();
		},
		methods: {
			getdata(){
				var that=this;
				uni.showLoading({
				    title: '数据加载中'
				});
				uni.request({
				    url: app.globalData.url+"user/xufei",
					method:"POST",
					dataType:"json",
					header: {
					    'content-type': 'application/x-www-form-urlencoded'
					},
				    data: {uid:that.uid},
				    success: (res) => {
						uni.hideLoading();
						if(res.data.code!=1){
							app.error_msg(res.data.msg);
						}
						that.price=res.data.data.price
						that.user=res.data.data.user
						that.show=1;
				    }
				});
			},
			moren(e){
				this.paytype=e.detail.value;
			},
			moren2(e){
				this.id=this.price[e.detail.value].id;
				this.money=this.price[e.detail.value].money;
			},
			chong(){
				uni.navigateTo({
				    url: "/pages/user/chong?type=1"
				});
			},
			shouquan(){
				var that=this;
				if(this.id==0){
					uni.showToast({
					    title: "请选择续费时长",
						icon:'none',
					    duration: 1500
					});
					return false;
				}
				uni.showLoading({
				    title: '请求中'
				});
				uni.login({
				  provider: 'weixin',
				  success: function (loginRes) {
					that.code=loginRes.code;
					that.baocun(1);
				  },
				  fail(){
					  uni.showToast({
					      title: '信息异常，请重试',
							icon:'none',
					      duration: 1000
					  });
				  }
				});
			},
			baocun(t=0){
				var that=this;
				if(this.id==0){
					uni.showToast({
					    title: "请选择续费时长",
						icon:'none',
					    duration: 1500
					});
					return false;
				}
				var zdtype='wxxcx';
				
				// #ifdef APP-PLUS
				if(that.zdtype==1){
					var zdtype='ios_app';
					that.paytype=3;
				}else{
					if(that.paytype==0){
						uni.hideLoading();
						uni.showToast({
						    title: "请选择支付方式",
							icon:'none',
						    duration: 1500
						});
						return false;
					}
					var zdtype='az_app';
				}
				// #endif
				
				
				uni.showLoading({
				    title: '请求中'
				});
				
				uni.request({
				    url: app.globalData.url+"user/doxufei",
					method:"POST",
					dataType:"json",
					header: {
					    'content-type': 'application/x-www-form-urlencoded'
					},
				    data: {uid:that.uid,id:that.id,paytype:that.paytype,zdtype:zdtype,code:that.code},
				    success: (res) => {
						uni.hideLoading();
						if(res.data.code!=1){
							uni.showToast({
							    title: res.data.msg,
								icon:'none',
							    duration: 1500
							});
							return false;
						}
						if(res.data.data.is_pay==0){
							uni.showToast({
							    title: res.data.msg,
								icon:'success',
							    duration: 1000
							});
							setTimeout(function(){
								uni.navigateBack({
								    delta: 1
								});
							},1000)
							return false;
						}
						if(zdtype=='az_app'){
							if(that.paytype==1){
								uni.requestPayment({
								    provider: 'wxpay',
								    orderInfo: {
										"appid": res.data.data.pay.appid,
										"noncestr": res.data.data.pay.noncestr, // 随机字符串
										"package": res.data.data.pay.package,        // 固定值
										"partnerid": res.data.data.pay.partnerid,      // 微信支付商户号
										"prepayid": res.data.data.pay.prepayid, // 统一下单订单号 
										"timestamp": res.data.data.pay.timestamp,        // 时间戳（单位：秒）
										"sign": res.data.data.pay.sign // 签名，这里用的 MD5 签名
									},
								    success: function (res2) {
								        uni.showToast({
								            title: "续费成功",
								        	icon:'success',
								            duration: 1000
								        });
								        setTimeout(function(){
								        	uni.navigateBack({
								        	    delta: 1
								        	});
								        },1000)
								    },
								    fail: function (err) {
								        console.log('fail:' + JSON.stringify(err));
								    }
								});
							}else{
								uni.requestPayment({
								    provider: 'alipay',
								    orderInfo: res.data.data.pay,
								    success: function (res2) {
										uni.showToast({
										    title: "续费成功",
											icon:'success',
										    duration: 1000
										});
										setTimeout(function(){
											uni.navigateBack({
											    delta: 1
											});
										},1000)
								    },
								    fail: function (err) {
								        console.log('fail:' + JSON.stringify(err));
								    }
								});
							}
						}else if(zdtype=='wxxcx'){
							uni.requestPayment({
							    provider: 'wxpay',
							    timeStamp: res.data.data.pay.timeStamp,
								nonceStr: res.data.data.pay.nonceStr,
								package: res.data.data.pay.package,
								signType: 'MD5',
								paySign: res.data.data.pay.paySign,
							    success: function (res) {
							        uni.showToast({
							            title: "续费成功",
							        	icon:'success',
							            duration: 1000
							        });
							        setTimeout(function(){
							        	uni.navigateBack({
							        	    delta: 1
							        	});
							        },1000)
							    },
							    fail: function (err) {
							        console.log('fail:' + JSON.stringify(err));
							    }
							});
						}else{
							uni.showToast({
							    title: "参数错误",
								icon:'error',
							    duration: 1500
							});
						}
				    }
				});
			},
			smoney(m){
				this.user.money=m;
			}
		}
	}
</script>

<style>
	.zhifu{width:100%;height:30px;line-height: 30px;margin-top: 20px;margin-bottom: 10px;color:red;}
	.info3{width:100%;height:45px;flex:1;}
	.info2 text{color:red;}
	.info2{width:80px;height:45px;line-height: 45px;margin-right: 5px;font-weight: 550;}
	.info{width:100%;display: flex;border-top: 1px rgb(245, 245, 245) solid;margin-top: 20px;}
	.info4{width:100%;height:40px;line-height: 40px;text-align: center;background-color: #05c160;border-radius: 5px;color:#fff;}
	.xuanze{width:100%;height:35px;line-height: 35px;display: block;}
	.main{width:94%;margin: 10px auto;}
</style>
